package cn.com.dhcc.app.pub.module.resmgr.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.annotation.CoreMenuCode;
import cn.com.dhcc.app.core.annotation.CoreMenuCode.CORE_LEAF_MENU;
import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.base.log.LogType;
import cn.com.dhcc.app.core.component.log.Log;
import cn.com.dhcc.app.core.exception.IllegalUserSessionException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.tag.CommonStaticCode.OPERATION_TYPE;
import cn.com.dhcc.app.pub.module.resmgr.service.ResourceService;
import cn.com.dhcc.app.pub.module.resmgr.vo.LinkResource;
import cn.com.dhcc.app.pub.module.resmgr.vo.MenuResource;
/**
 * 资源管理
 * @日期：2013-5-30下午3:55:15
 * @作者：乔兵
 * @版权所有：东华软件
 * @版本：1.0
 */
@Controller
@RequestMapping("pub/sysmgr/resmgr")
@CoreMenuCode(belongToMenu=CORE_LEAF_MENU.RES_MGR)
public class ResourceMgrController extends BaseController{

	@Autowired
	private ResourceService resourceService;
	Logger logger = Log.getLogger(LogType.BUSILOG);
	
	@RequestMapping("res_list")
	public String resourceList(Model model, HttpSession httpSession, String refreshNode){
		try{
			model.addAttribute("resourceData", resourceService.reloadTree());
			if(refreshNode!=null){
				model.addAttribute("refreshNode", refreshNode);
			}else{
				model.addAttribute("refreshNode", "");
			}
			return "pub/sysmgr/resmgr/res_info_list";
		} catch (ServiceException e) {
			return toAlertPage(model,"获取资源失败");
		}
	}
	/**
	 * 
	 * @param model
	 * @param operation
	 * @param currentNodeId
	 * @return
	 */
	@RequestMapping("res_add")
	public String resourceAdd(Model model, String selectedNodeId, String operationType, String type){
		try{
			model.addAttribute("selectedNodeId", selectedNodeId);
			model.addAttribute("operationType", operationType);
			model.addAttribute("type", type);
			if(MenuResource.ResourceType.ACTION.getCode().equalsIgnoreCase(type)){
				List<MenuResource> groupForShare = resourceService.getLinkGroupForLinkShare(selectedNodeId,OPERATION_TYPE.ADD);
				
				model.addAttribute("groups", groupForShare);
				
				return "pub/sysmgr/resmgr/res_link_info_add";
			}else if(MenuResource.ResourceType.ACTION_GROUP.getCode().equalsIgnoreCase(type)){
				Integer maxOrderNum = resourceService.getMenuMaxOrderNum(selectedNodeId);
				model.addAttribute("next_order",maxOrderNum==0?1:maxOrderNum+1);
				return "pub/sysmgr/resmgr/res_link_group_add";
			}else{
				return "pub/sysmgr/resmgr/res_menu_info_add";
			}
		} catch (ServiceException e) {
			return toAlertPage(model, "操作失败");
		}
	}
	@RequestMapping("add_res")
	@ResponseBody
	public Map<String, Object> addResource(HttpServletResponse response,Model model,MenuResource menuResource,String[] linkGroups,LinkResource linkResource,String selectedNodeId,String operationType,String type){
		try{
			resourceService.addResource(menuResource,linkGroups,linkResource,selectedNodeId,operationType,type);
			return jsonSuccessResult("添加成功");
		} catch (NiceException e) {
			return jsonFailResult("添加资源失败! "+e.getMessage());
		}catch (ServiceException e) {
			return jsonFailResult("添加资源失败! ");
		}catch (Exception e) {
			logger.error(e);
			return jsonFailResult("添加资源失败! ");
		}
	}
	@RequestMapping("res_detail")
	public String resourceDetail(Model model,String selectedNodeId,String type){
		try{
			if(MenuResource.ResourceType.ACTION.getCode().equalsIgnoreCase(type)){
				LinkResource linkResource = resourceService.getLinkResourceById(selectedNodeId);
				List<MenuResource> groupForShare = resourceService.getLinkGroupForLinkShare(selectedNodeId,OPERATION_TYPE.GET);
				model.addAttribute("groups", groupForShare);
				model.addAttribute("res", linkResource);
				return "pub/sysmgr/resmgr/res_link_info_get";
			}else if(MenuResource.ResourceType.ACTION_GROUP.getCode().equalsIgnoreCase(type)){
				MenuResource menuResource = resourceService.getMenuResourceById(selectedNodeId);
				model.addAttribute("res", menuResource);
				return "pub/sysmgr/resmgr/res_link_group_get";
			}else{
				MenuResource menuResource = resourceService.getMenuResourceById(selectedNodeId);
				model.addAttribute("res", menuResource);
				return "pub/sysmgr/resmgr/res_menu_info_get";
			}
			
		} catch (ServiceException e) {
			return toAlertPage(model, "获取资源失败");
		}
	}
	@RequestMapping("res_update")
	public String resourceUpdate(Model model,String selectedNodeId,String type){
		try{
			model.addAttribute("type", type);
			model.addAttribute("selectedNodeId", selectedNodeId);
			if(MenuResource.ResourceType.ACTION.getCode().equalsIgnoreCase(type)){
				LinkResource linkResource = resourceService.getLinkResourceById(selectedNodeId);
				List<MenuResource> groupForShare = resourceService.getLinkGroupForLinkShare(selectedNodeId,OPERATION_TYPE.UPDATE);
				model.addAttribute("groups", groupForShare);
				model.addAttribute("res", linkResource);
				return "pub/sysmgr/resmgr/res_link_info_update";
			}else if(MenuResource.ResourceType.ACTION_GROUP.getCode().equalsIgnoreCase(type)){
				MenuResource menuResource = resourceService.getMenuResourceById(selectedNodeId);
				model.addAttribute("res", menuResource);
				return "pub/sysmgr/resmgr/res_link_group_update";
			}else{
				MenuResource menuResource = resourceService.getMenuResourceById(selectedNodeId);
				model.addAttribute("res", menuResource);
				return "pub/sysmgr/resmgr/res_menu_info_update";
			}
		} catch (ServiceException e) {
			return toAlertPage(model, "获取资源失败");
		}
	}
	@RequestMapping("update_res")
	@ResponseBody
	public Map<String, Object> updateResource(Model model,String id,MenuResource menuResource,String[] linkGroups,LinkResource linkResource,String type){
		try{
			if(MenuResource.ResourceType.ACTION.getCode().equalsIgnoreCase(type)){
				resourceService.updateLinkResource(linkResource,linkGroups);
			}else{
				resourceService.updateMenuResource(menuResource);
			}
		
			return jsonSuccessResult("修改成功");
		} catch (ServiceException e) {
			return jsonFailResult("修改失败");
		} catch (IllegalUserSessionException e) {
			return jsonFailResult("请登录!");
		}
	}
	@RequestMapping("res_action_set")
	public String resourceActionSet(Model model,String selectedNodeId,String type){
		try{
			model.addAttribute("resourceData", resourceService.getMenusResource(selectedNodeId));			
			model.addAttribute("res", resourceService.getMenuResourceById(selectedNodeId));
			return "pub/sysmgr/resmgr/res_action_set";
		} catch (ServiceException e) {
			return toAlertPage(model, "获取资源失败");
		}
	}
	@RequestMapping("set_action")
	@ResponseBody
	public Map<String, Object> setResourceAction(Model model,MenuResource menuResource,String actionId){
		try{
			resourceService.setMenuAction(menuResource,actionId);
			return jsonSuccessResult("设置成功");
		} catch (ServiceException e) {
			return jsonFailResult("设置失败");
		}
	}
	//set_initaction
	@RequestMapping("set_initaction")
	@ResponseBody
	public Map<String, Object> setInitaction(Model model,String selectedNodeId){
		try{
			resourceService.setInitaction(selectedNodeId);
			return jsonSuccessResult("设置成功");
		} catch (ServiceException e) {
			return jsonFailResult("设置失败");
		}
	}
	@RequestMapping("delete_res")
	@ResponseBody
	public Map<String, Object> deleteResource(Model model,String selectedNodeId,String type){
		try{
			if(MenuResource.ResourceType.ACTION.getCode().equalsIgnoreCase(type)){
				resourceService.deleteLinkResource(selectedNodeId);
			}else{
				resourceService.deleteMenuResource(selectedNodeId);
			}
			return jsonSuccessResult("删除成功");
		} catch (ServiceException e) {
			return jsonFailResult("删除失败");
		}
	}
	@RequestMapping("change_res_order")
	@ResponseBody
	public Map<String, Object> changeResourceOrder(Model model,String selectedNodeId,String type){
		try{
			resourceService.changeResourceOrder(selectedNodeId,type);
			return jsonSuccessResult("移动成功");
		} catch (ServiceException e) {
			return jsonFailResult("移动失败");
		} catch (NiceException e) {
			return jsonFailResult("移动失败"+e.getMessage());
		}
	}
	@RequestMapping("is_res_id_unique")
	@ResponseBody
	public String checkResId(String id){
		try {
			if (resourceService.checkResId(id)) {
				return FORM_CHECK_FAIL;
			} else {
				return FORM_CHECK_SUCCESS;
			}
		} catch (ServiceException e) {
			return FORM_CHECK_FAIL;
		}
	}
}
